﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;

namespace GGJ.Sprites
{
    class HoneyPot : Ennemy
    {
        private int tileWidth = 160;
        private int tileHeight = 174;

        private Rectangle detectionDestination;

        public HoneyPot(Texture2D texture, int x, int y)
            : base(texture)
        {
            destination = new Rectangle(
                x,
                y,
                tileWidth,
                tileHeight);

            detectionDestination = new Rectangle(
                x - 200,
                y - 200,
                tileWidth + 200,
                tileHeight + 200);
        }

        public override void Update(GameTime gameTime, Flies flies, KilledFlies killedFlies, float speed, bool isSlowMotion)
        {
            destination.X -= (int)speed;
            detectionDestination.X -= (int)speed;
            Rectangle hitbox = new Rectangle(destination.X , 0, destination.Width, 600);

            bool isForcedToGoDown = false;

            Fly fly;

            Rectangle hitbox2 = new Rectangle(destination.X, destination.Y + (destination.Height / 4), destination.Width, 3 * (destination.Height / 4));

            for (int i = 0; i < flies.Count(); i++)
            {
                fly = flies.GetFly(i);

                if (fly != null)
                {
                    if (hitbox2.Intersects(fly.GetDestination()))
                    {
                        //registered utilisé quand on fait disparaitre un mort directement
                        killedFlies.registeredLost();
                        flies.removeFly(i);
                    }
                    else if (detectionDestination.Intersects(fly.GetDestination()))
                    {
                        isForcedToGoDown = true;
                    }
                }
            }

            flies.setIsForcedToGoDown(isForcedToGoDown);
        }

        public override void Draw(SpriteBatch spriteBatch)
        {
            Rectangle source;

            source = new Rectangle(0,
                308,
                tileWidth,
                tileHeight);

            spriteBatch.Draw(texture, destination, source, Color.White);
        }
    }
}
